package testLayer;

import static org.junit.Assert.*;

import java.util.Date;

import modelLayer.Student;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import controlLayer.StudentCtr;
import dbLayer.DBClass;
import dbLayer.DBStudent;
import dbLayer.DBTeacher;

public class StudentControllerTest {
	
	private DBStudent dbStudent;
	private StudentCtr studentCtr;
	private boolean retrieveAssociation = true;
	
	@BeforeClass
	public static void prep() throws Exception {
		
		new DBClass().insert("men0813", "1", "2013", new DBTeacher().findById(1));
		
	}
	
	@AfterClass
	public static void done() throws Exception {
		
		new DBClass().remove(new DBClass().find("men0813", false).getId());
		
	}
	
	@Before
	public void setUp() throws Exception {
		
		this.dbStudent = new DBStudent();
		this.studentCtr = new StudentCtr();
		this.createStudent();
		
	}
	
	@After
	public void tearDown() throws Exception {
		
		this.removeStudent();
		this.dbStudent = null;
		this.studentCtr = null;

		
	}
	
	public void createStudent() throws Exception {
		
		this.studentCtr.createStudent("Jakob Bramsen", "21355487-3698", "Byvejen 111, 9000 Aalborg", 
				 				  		  "12365478", "JakobsMor@gmail.com", new Date(), false, 
				 				  		  new DBClass().find("men0813", false));	 
		
	}
	
	@Test
	public void findStudentByName() throws Exception {
		
		if (retrieveAssociation) {
			assertTrue(this.studentCtr.findStudentByName("Jakob Bramsen", retrieveAssociation).get(0).getCl() != null);
		}
		else {
			assertTrue(this.studentCtr.findStudentByName("Jakob Bramsen", retrieveAssociation).get(0) != null);
		}
		
	}
	
	@Test
	public void findStudentById() throws Exception {
		
		if (retrieveAssociation) {
			assertTrue(this.studentCtr.findStudentById(this.dbStudent.find("Jakob Bramsen", false).get(0).getId(),
													   retrieveAssociation).getCl() != null);
		}
		else {
			assertTrue(this.studentCtr.findStudentById(this.dbStudent.find("Jakob Bramsen", false).get(0).getId(),
													   retrieveAssociation) != null);
		}
		
	}
	
	@Test
	public void getAll() throws Exception {
		
		boolean test = true;
		if (retrieveAssociation) {			
			for (Student s : this.studentCtr.getAll(retrieveAssociation)) {
				if (s.getCl() == null) {
					test = false;
				}
			}
		}
		else {
			if (this.studentCtr.getAll(retrieveAssociation).isEmpty() &&
				!(this.studentCtr.getAll(retrieveAssociation).size() >= 1)) {
				test = false;
			}
		}
		
		assertTrue(test);
	}
	
	@Test
	public void updateStudent() throws Exception {
		
		assertTrue(this.studentCtr.updateStudent(this.studentCtr.findStudentByName("Jakob Bramsen", true).get(0)) > 0);
		
	}
	
	public void removeStudent() throws Exception {
		
		this.studentCtr.removeStudent(this.dbStudent.find("Jakob Bramsen", false).get(0));
		
	} 

}
